package com.alibaba.travel.thirdpart.service.impl;

import com.alibaba.travel.common.model.dto.TopicLikesCountResponse;
import com.alibaba.travel.common.utils.RRException;
import com.alibaba.travel.thirdpart.interceptor.LoginInterceptor;
import com.alibaba.travel.thirdpart.scheduled.LikesRefreshScheduled;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.alibaba.travel.common.utils.PageUtils;
import com.alibaba.travel.common.utils.Query;

import com.alibaba.travel.thirdpart.dao.UserLikesRecordDao;
import com.alibaba.travel.thirdpart.entity.UserLikesRecordEntity;
import com.alibaba.travel.thirdpart.service.UserLikesRecordService;
import org.springframework.util.ObjectUtils;


@Service("userLikesRecordService")
public class UserLikesRecordServiceImpl extends ServiceImpl<UserLikesRecordDao, UserLikesRecordEntity> implements UserLikesRecordService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        if (ObjectUtils.isEmpty(params.get("topicType"))) {
            throw new RRException("必须确定topicType参数");
        }
        IPage<UserLikesRecordEntity> page = this.page(
                new Query<UserLikesRecordEntity>().getPage(params),
                new LambdaQueryWrapper<UserLikesRecordEntity>()
                        .eq(UserLikesRecordEntity::getUserId, LoginInterceptor.userIdLocal.get())
                        .eq(UserLikesRecordEntity::getTopicType, params.get("topicType").toString())
        );
        LoginInterceptor.userIdLocal.remove();
        return new PageUtils(page);
    }


    @Autowired
    private LikesRefreshScheduled likesRefreshScheduled;

    @Override
    public List<TopicLikesCountResponse> getLikesNumber(Long[] topicIds) {
        List<TopicLikesCountResponse> countList= new ArrayList<>();
        for (Long topicId : topicIds) {
            long count = this.count(
                    new LambdaQueryWrapper<UserLikesRecordEntity>()
                            .eq(UserLikesRecordEntity::getTopicId, topicId));
            countList.add(new TopicLikesCountResponse(topicId, count));
        }
        return countList;
    }

    /**
     * 已登录用户浏览页面，展示当前所有点赞状态
     * @return
     * @param topicType
     */
    @Override
    public List<UserLikesRecordEntity> getStatus(String topicType) {
        // 1.更新Redis信息进入数据库
        likesRefreshScheduled.refreshStatus();
        // 2.查询数据库信息 防止冲突
        List<UserLikesRecordEntity> status = this.baseMapper.selectList(new LambdaQueryWrapper<UserLikesRecordEntity>()
                .eq(UserLikesRecordEntity::getUserId, LoginInterceptor.userIdLocal.get())
                .eq(UserLikesRecordEntity::getTopicType, topicType));

        return status;
    }

}